package com.int_tech.utils.poi;

import com.int_tech.car.entity.Car;
import com.int_tech.car.entity.City;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/10/23.
 */
public class ReadExcelCarCeiling {
    public static List<Car> readExcel(MultipartFile file,List<City> cityList) {
        List<Car> list = new ArrayList<>();
        XSSFWorkbook workbook = null;
        CarMap carMap = new CarMap();
        HashMap<String, String> map = carMap.carMap(cityList);
        try {
            // 读取Excel文件
            InputStream inputStream = file.getInputStream();
            workbook = new XSSFWorkbook(inputStream);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 循环工作表
        for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
            XSSFSheet sheet = workbook.getSheetAt(numSheet);
            if (sheet == null) {
                continue;
            }
            // 循环行
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                XSSFRow row = sheet.getRow(rowNum);
                if (row == null) {
                    continue;
                }

                // 将单元格中的内容存入集合
                Car car = new Car();

                XSSFCell cell = row.getCell(0);
                if (cell == null) {
                    continue;
                }
                String carNumber = cell.getStringCellValue();
                String substring = carNumber.substring(0, 1);
                if (substring.equals("0")) {
                    car.setCarNumber(carNumber);
                    String substring1 = carNumber.substring(0, 3);
                    String city = map.get(substring1);
                    if (city == null) {
                        continue;
                    }
                    String substring2 = carNumber.substring(3, carNumber.length());
                    String carCnumber = city + substring2;
                    car.setCarCnumber(carCnumber);
                }else if(!substring.equals("0")){
                    car.setCarCnumber(carNumber);
                    String substring1 = carNumber.substring(0, 1);
                    String key = CarMap.getKey(map, substring1);
                    if (key == null){
                        continue;
                    }
                    String substring2 = carNumber.substring(1, carNumber.length());
                    String carCnumber = key + substring2;
                    car.setCarNumber(carCnumber);
                }

                cell = row.getCell(1);
                if (cell == null) {
                    continue;
                }
                car.setStationName(cell.getStringCellValue());

                cell = row.getCell(2);
                if (cell == null) {
                    continue;
                }
                car.setCarBrand(cell.getStringCellValue());

                cell = row.getCell(3);
                if (cell == null) {
                    continue;
                }
                car.setModel(cell.getStringCellValue());

                cell = row.getCell(4);
                if (cell == null) {
                    continue;
                }

//                Double sss = cell.getNumericCellValue();

                car.setLength(cell.getStringCellValue());

                cell = row.getCell(5);
                if (cell == null) {
                    continue;
                }
                car.setWidth(cell.getStringCellValue());

                cell = row.getCell(6);
                if (cell == null) {
                    continue;
                }
                car.setHeight(cell.getStringCellValue());

                cell = row.getCell(7);
                if (cell == null) {
                    continue;
                }
                car.setLoud(cell.getStringCellValue());

                list.add(car);
            }
        }
        map.clear();
        return list;
    }
}
